package com.zito.chat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
import org.comet4j.core.util.JSONUtil;

import com.zito.chat.dto.TalkDTO;
import com.zito.chat.dto.UserDTO;


public class ChatServlet extends HttpServlet {
    /** serialVersionUID */
    private static final long serialVersionUID = -1311176251844328163L;
    private static final String CMD_FLAG = "cmd";
    private static final String TALK_CMD = "talk";
    private static final String LIST_CMD = "list";
    private static final CometContext context = CometContext.getInstance();
    private static final CometEngine engine = context.getEngine();
    private static final AppStore appStore = AppStore.getInstance();
    
    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                    IOException {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            String cmd = request.getParameter(CMD_FLAG);
            // 发送信息
            if (TALK_CMD.equals(cmd)) {
                    String id = request.getParameter("id");
                    
                    String name = appStore.get(id);
                    
                    String text = request.getParameter("text");
                    TalkDTO dto = new TalkDTO(id, name, text);
                    engine.sendToAll(Constant.APP_CHANNEL, dto);
                    return;
            }
            // 在线列表
            if (LIST_CMD.equals(cmd)) {
                    List<UserDTO> userList = new ArrayList<UserDTO>();
                    Map<String, String> map = AppStore.getInstance().getMap();
                    Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
                    while (iter.hasNext()) {
                            Map.Entry<String, String> entry = iter.next();
                            String id = entry.getKey();
                            String name = entry.getValue();
                            userList.add(new UserDTO(id, name));
                    }
                    String json = JSONUtil.convertToJson(userList);
                    
                    System.out.println(json);
                    response.getWriter().print(json);
            }
    }
}
